<?php
/**
 *
 * @author Rodrigo Régis Palmeira <regisbsb@gmail.com>
 *
 */
namespace Foxforms\Widget;
class Field extends \Foxforms\Element
{
	protected $placeHolder;
	protected $readOnly;
	protected $disabled;
	protected $validate;
	protected $size;
	protected $maxLength;

	public function __construct($name = null)
	{
		$this->setName($name);
	}

	/**
	 * Set the placeHolder of a input
	 * @param string $value
	 * @return FoxInputForms
	 */
	public function setPlaceHolder($placeHolder)
	{
		$this->placeHolder = $placeHolder;
		return $this;
	}

	/**
	 * Define the readyOnly attribute of a input
	 * @param string $value
	 * @return FoxInputForms
	 */
	public function readOnly($bool = true)
	{
		$this->readOnly = $bool;
		return $this;
	}

	/**
	 * Define the disabled attribute of a input
	 * @param string $value
	 * @return FoxInputForms
	 */
	public function disabled($bool = true)
	{
		$this->disabled = $bool;
		return $this;
	}

	public function setMaxLength($maxLength)
	{
		$this->maxLength = $maxLength;
		return $this;
	}

	/**
	 * Set the input as an needed value
	 * @param string $value
	 * @return FoxInputForms
	 */
	public function validate($bool = true)
	{
		$this->validate = $bool;
		if ($this->validate)
			$this->addClass('required');
		return $this;
	}

	public function setSize($size)
	{
		if ($this instanceof FoxTextAreaForms)
		{
			$size = explode('x', $size);
			$this->setCols($size[0])->setRows($size[1]);
		}
		else
			$this->size = $size;

		return $this;
	}

	public static function createField($name, $data = array())
	{
		if ($data['type'] && (!isset($data['length']) || empty($data['length'])))
			$field	= new \FoxForms\FoxTextAreaForms($name);
		else if ($data['type'] == 'string')
			$field	= new \FoxForms\FoxInputForms($name);
		else if ($data['type'] == 'integer')
			$field	= new \FoxForms\FoxInputForms($name);

		if (isset($data['notnull']))
			$field->validate($data['notnull']);

		if (isset($data['length']))
			$field->setMaxLength($data['length']);

		return $field;

// 		echo '<pre>'; print_r($field);die;
// 		echo '<pre>'; print_r($data);die;
	}
}